<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="库房编号" prop="code">-->
<!--        <el-input-->
<!--          v-model="queryParams.code"-->
<!--          placeholder="请输入库房编号"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="库存组织" prop="orgguid">
        <el-select v-model="queryParams.orgguid" placeholder="请选择库存组织" filterable clearable size="small">
          <el-option
            v-for="item in orgOptions"
            :key="item.guid"
            :label="item.name"
            :value="item.guid"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="库房名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入库房名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="库房性质" prop="nature">
        <el-select v-model="queryParams.nature" placeholder="请选择库房性质" clearable size="small">
          <el-option
            v-for="dict in natureOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="联系人" prop="contact">
        <el-input
          v-model="queryParams.contact"
          placeholder="请输入联系人"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="contacttel">
        <el-input
          v-model="queryParams.contacttel"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="状态" prop="enable">
        <el-select v-model="queryParams.enable" placeholder="请选择状态" clearable size="small">
          <el-option
            v-for="dict in enableOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="分组编码" prop="businesscode">-->
<!--        <el-input-->
<!--          v-model="queryParams.businesscode"-->
<!--          placeholder="请输入分组编码"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="发货标识" prop="send">
        <el-select v-model="queryParams.send" placeholder="请选择发货标识" clearable size="small">
          <el-option
            v-for="dict in sendOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="金蝶编号" prop="kingdeenum">
          <el-input v-model="queryParams.kingdeenum" placeholder="请输入金蝶编号" />
        </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['store:storeroom:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['store:storeroom:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['store:storeroom:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['store:storeroom:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="storeroomList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="库房编号" align="center" prop="code" />-->
      <el-table-column label="库房名称" align="center" prop="name" />
      <el-table-column label="地址" align="center" prop="province" >
         <template slot-scope="scope">
          <span>{{scope.row.province}}-{{scope.row.city}}-{{scope.row.area}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="区县" align="center" prop="area" /> -->
      <el-table-column label="详细地址" align="center" prop="address" />
      <el-table-column label="联系人" align="center" prop="contact" />
      <el-table-column label="联系电话" align="center" prop="contacttel" />
      <el-table-column label="状态" align="center" prop="enable" :formatter="enableFormat" />
<!--      <el-table-column label="分组编码" align="center" prop="businesscode" />-->
      <el-table-column label="发货标识" align="center" prop="send" :formatter="sendFormat" />
      <!-- <el-table-column label="创建时间" align="center" prop="createdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人" align="center" prop="operatorid" />
      <el-table-column label="最后更新时间" align="center" prop="lastupdate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastupdate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['store:storeroom:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:storeroom:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改库房管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" :inline="true" label-width="120px">
<!--        <el-form-item label="库房编号" prop="code">-->
<!--          <el-input v-model="form.code" placeholder="请输入库房编号" clearable />-->
<!--        </el-form-item>-->
        <el-form-item label="库存组织" prop="orgguid" >
          <el-select v-model="form.orgguid" placeholder="请选择库存组织" filterable clearable :disabled="form.whinfoData">
            <el-option
              v-for="item in orgOptions"
              :key="item.guid"
              :label="item.name"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="库房名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入库房名称" clearable />
        </el-form-item>
        <el-form-item label="库房管理人员" prop="managerid">
          <!-- <el-input v-model="form.managerid" placeholder="请输入库房管理人员" /> -->
          <el-select v-model="form.managerid" placeholder="请选择用户" filterable clearable>
            <el-option
              v-for="item in usersOptions"
              :key="item.guid"
              :label="item.realname"
              :value="item.guid"
            />
        </el-select>
        </el-form-item>
        <el-form-item label="库房性质" prop="nature">
          <el-select v-model="form.nature" placeholder="请选择库房性质" :disabled="form.whinfoData" clearable >
            <el-option
              v-for="dict in natureOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="生产品类" prop="categoryids">
          <!-- <el-input v-model="form.categoryids" placeholder="请输入品类id" /> -->
           <el-select v-model="form.categoryids" placeholder="请选择生产品类" clearable multiple style="width:100%">
            <el-option
              v-for="item in categoryOption"
              :key="item.guid"
              :label="item.name"
              :value="item.guid"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="金蝶编号" prop="kingdeenum">
          <el-input v-model="form.kingdeenum" placeholder="请输入金蝶编号" clearable />
        </el-form-item>
        <el-form-item label="地址" prop="province">
          <!-- <el-cascader size="large" :options="options" v-model="form.add"  @change="handleChange"> </el-cascader> -->
          <v-distpicker :province="form.province" :city="form.city" :area="form.area" @selected="onSelected"></v-distpicker>
        </el-form-item>
        <br>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" clearable />
        </el-form-item>
<!--
        <el-form-item label="经度" prop="longitude">
          <el-input v-model="form.longitude" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input v-model="form.latitude" placeholder="请输入纬度" />
        </el-form-item> -->
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="form.contact" placeholder="请输入联系人" clearable />
        </el-form-item>
        <el-form-item label="联系电话" prop="contacttel">
          <el-input v-model="form.contacttel" placeholder="请输入联系电话" clearable />
        </el-form-item>
<!--        <el-form-item label="标准编号" prop="standardcode">-->
<!--          <el-input v-model="form.standardcode" placeholder="请输入标准编号" clearable />-->
<!--        </el-form-item>-->
        <el-form-item label="状态" prop="enable">
          <el-select v-model="form.enable" placeholder="请选择状态" clearable>
            <el-option
              v-for="dict in enableOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工厂类型" prop="flag">
          <el-select v-model="form.flag" placeholder="请选择工厂类型" clearable>
            <el-option
              v-for="dict in storeTypeOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="分组编码" prop="businesscode">-->
<!--          <el-input v-model="form.businesscode" placeholder="请输入分组编码" clearable />-->
<!--        </el-form-item>-->
        <el-form-item label="发货标识" prop="send">
          <el-select v-model="form.send" placeholder="请选择发货标识" clearable>
            <el-option
              v-for="dict in sendOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" clearable style="width: 543px;"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listStoreroom, getStoreroom, delStoreroom, addStoreroom, updateStoreroom, exportStoreroom } from "@/api/store/storeroom";
import { tuserList, findOrganizationList,findCategoryList } from "@/api/public";

export default {
  name: "Storeroom",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 库房管理表格数据
      storeroomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 库房性质字典
      natureOptions: [],
      // 状态字典
      enableOptions: [],
      // 状态字典
      storeTypeOptions: [],
      // 类型字典
      typeOptions: [],
      // 是否发货标识: 1-发货  2-不发货字典
      sendOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        managerid: null,
        capacity: null,
        nature: null,
        province: null,
        city: null,
        area: null,
        createdate: null,
        operatorid: null,
        lastupdate: null,
        contact: null,
        contacttel: null,
        orgguid: null,
        standardcode: null,
        enable: null,
        type: 1,
        arealandid: null,
        businesscode: null,
        send: null,
        kingdeenum: null,
      },
      // 表单参数
      form: {},
      rules: {
        name: [
          { required: true, message: "库房名称不能为空", trigger: "blur" }
        ],
        contact: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        contacttel: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        send: [
          { required: true, message: "发货标识不能为空", trigger: "change" }
        ],
        enable: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
        orgguid: [
          { required: true, message: "库存组织不能为空", trigger: "change" }
        ]
      },
      usersOptions: [],
      orgOptions: [],
      dis : false,
      categoryOption: [],
      flag:null,
    };
  },
  created() {
    this.getList();
    this.findOrganizationList();
    this.findCategoryList();
    this.userList();
    this.getDicts("store_nature").then(response => {
      this.natureOptions = response.data;
    });
    this.getDicts("store_enable").then(response => {
      this.enableOptions = response.data;
    });
    this.getDicts("sotre_flag").then(response => {
      this.storeTypeOptions = response.data;
    });
    this.getDicts("store_type").then(response => {
      this.typeOptions = response.data;
    });
    this.getDicts("store_send").then(response => {
      this.sendOptions = response.data;
    });
  },
  methods: {
    //获取分类列表
    findCategoryList(){
      findCategoryList().then(response => {
        this.categoryOption = response.data;
      });
    },
    onSelected(data) {
      this.form.province = data.province.value
      this.form.city = data.city.value
      this.form.area = data.area.value
    },
    //查询组织
    findOrganizationList(){
      findOrganizationList().then(response => {
        this.orgOptions = response.data;
      });
    },
    //查询用户
    userList(){
      tuserList().then(response => {
        this.usersOptions = response.data;
      });
    },

    /** 查询库房管理列表 */
    getList() {
      this.loading = true;
      listStoreroom(this.queryParams).then(response => {
        this.storeroomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 库房性质字典翻译
    natureFormat(row, column) {
      return this.selectDictLabel(this.natureOptions, row.nature);
    },
    // 状态字典翻译
    enableFormat(row, column) {
      return this.selectDictLabel(this.enableOptions, row.enable);
    },
    // 类型字典翻译
    typeFormat(row, column) {
      return this.selectDictLabel(this.typeOptions, row.type);
    },
    // 是否发货标识: 1-发货  2-不发货字典翻译
    sendFormat(row, column) {
      return this.selectDictLabel(this.sendOptions, row.send);
    },
    enableFormat(row, column) {
      return this.selectDictLabel(this.storeTypeOptions, row.flag);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        guid: null,
        code: null,
        name: null,
        managerid: null,
        capacity: null,
        nature: null,
        address: null,
        province: null,
        city: null,
        area: null,
        remark: null,
        createdate: null,
        operatorid: null,
        lastupdate: null,
        longitude: null,
        latitude: null,
        contact: null,
        contacttel: null,
        orgguid: null,
        standardcode: null,
        enable: null,
        type: 1,
        farmerid: null,
        arealandid: null,
        businesscode: null,
        send: null,
        whinfoData: false,
        flag:null,
        categoryids:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.guid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加库房管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const guid = row.guid || this.ids
      getStoreroom(guid).then(response => {
        this.form = response.data;
        if(response.data.categoryids != null && response.data.categoryids != ""){
          this.form.categoryids = response.data.categoryids.split(",");
        }
        this.open = true;
        this.title = "修改库房管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.guid != null) {
            this.form.categoryids = this.form.categoryids.toString();
            updateStoreroom(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.categoryids = this.form.categoryids.toString();
            addStoreroom(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const guids = row.guid || this.ids;
      this.$confirm('是否确认删除该数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStoreroom(guids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有库房管理数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportStoreroom(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
